﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Coffee_Restaurant.DAL;
using Coffee_Restaurant.Database;

namespace Coffee_Restaurant.Forms
{
    public partial class ManagerPrinter : Form
    {
        //event edit
        public delegate void ButtonSaveEventHandler();
        public int? ItemCategoryId { get; set; }

        public ManagerPrinter()
        {
            InitializeComponent();
        }

        //esc to exit
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Escape)
            {
                this.Close();
                return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void Form_Load(object sender, EventArgs e)
        {
            btnSave.FlatAppearance.BorderColor = Color.FromArgb(0, 255, 255, 255); // trong suot

            foreach (string printer in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
            {
                cbbPrinter1.Items.Add(printer);
                cbbPrinter2.Items.Add(printer);
            }

            //default from database
            using (var db = new DatabaseEntities())
            {
                var printer = db.Printers.ToList();
                cbbPrinter1.SelectedItem = printer[0].PrinterName; // may in thanh toan
                cbbPrinter2.SelectedItem = printer[1].PrinterName; // may in bep
            }

            foreach (Button control in Controls.OfType<Button>())
            {
                if (control.Name != "btnClose")
                {
                    control.MouseHover += btn_MouseHover;
                    control.MouseLeave += btn_MouseLeave;
                }
            }
        }


        private void btn_MouseHover(object sender, EventArgs e)
        {
            var btn = sender as Button;
            if (btn != null)
            {
                btn.BackgroundImage = global::Coffee_Restaurant.Properties.Resources.woodbutton_hover;
            }
        }

        private void btn_MouseLeave(object sender, EventArgs e)
        {
            var btn = sender as Button;
            if (btn != null)
            {
                btn.BackgroundImage = global::Coffee_Restaurant.Properties.Resources.woodbutton;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                PrinterDa.Update(cbbPrinter1.Text, 1); // may in hoa don
                PrinterDa.Update(cbbPrinter2.Text, 2); // may in bep
                this.Hide();

                //default printer windows
                SetDefaultPrinter(cbbPrinter1.Text);

            }
            catch (Exception)
            {
            }
        }

        [DllImport("Winspool.drv")]
        private static extern bool SetDefaultPrinter(string printerName);

    }
}
